package com.yellowpage.nsp.action;
  
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;

import com.yellowpage.nsp.data.NspErrorCode;
import com.yellowpage.nsp.service.NspErrorCodeService;

public class NspErrorCodeAction extends BaseAction {

    public String RESULT_QUERY = "query";
    public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	
	private Configuration configuration;
	
	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}
	
	private List<NspErrorCode> nspErrorCodeList = new ArrayList<NspErrorCode>();
	private List<Map> resultList = new ArrayList<Map>();
	
	private NspErrorCode nspErrorCode;
	private NspErrorCodeService nspErrorCodeService;
	
	private String selectedId;	
	private String[] selectedIds;	
	private String message;
	
		private String query_errorCode;
		private String query_errorDesc;
		
	public void prepare() {
		super.prepare();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspErrorCode = nspErrorCodeService.getNspErrorCode(Long.parseLong(selectedId));
		}
	}
	
	public String queryByPage() {		
		initPageRender();
		
		Map queryMap = new HashMap();	
			    queryMap.put("errorCode", query_errorCode);
	    	    queryMap.put("errorDesc", query_errorDesc);
	    	    	
		resultList = nspErrorCodeService.queryNspErrorCodeByPage(queryMap, pageRender);
		
		setPageRenderToRequest();

		return RESULT_QUERY;
	}
	
	public String toAdd() {
		return RESULT_ADD;
	}
	
	public String save() {
	    
		nspErrorCodeService.saveNspErrorCode(nspErrorCode);		
		
		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {
	    	    	    	    	    if (nspErrorCode.getErrorDesc().length() == 0) {
			this.addActionError("** ErrorDesc 不能为空 !");
		}
			    	    
		this.setValidatorResult("/nsp/NspErrorCodeAdd.jsp");		
	}
	
	public String toEdit() {
	    Long id = new Long(selectedId);
	    
	    nspErrorCode = nspErrorCodeService.getNspErrorCode(id);
	
		return RESULT_EDIT;
	}
	
	public String update() {
	
	    nspErrorCodeService.updateNspErrorCode(nspErrorCode);	

		message = "修改成功!";
	    
		return RESULT_EDIT;
	}
	
	public void validateUpdate() {
					    			    if (nspErrorCode.getErrorDesc().length() == 0) {
			this.addActionError("** ErrorDesc 不能为空 !");
		}
			    		this.setValidatorResult("/nsp/NspErrorCodeUpdate.jsp");	
	}
	
	public String delete() {	    
	    List ids = new ArrayList();
	    
		for(String id : selectedIds){
			ids.add(Long.valueOf(id));
		}
		
		try {
			this.nspErrorCodeService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突，删除失败 !");
		}
		
		
		return queryByPage();
	}	
	
	

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	 public void setNspErrorCode(NspErrorCode nspErrorCode) {
		this.nspErrorCode = nspErrorCode;
	}
	
	public NspErrorCode getNspErrorCode() {
		return nspErrorCode;
	}
	 
	 
	 public void setNspErrorCodeService(NspErrorCodeService nspErrorCodeService) {
		this.nspErrorCodeService = nspErrorCodeService;
	}
	
	public List<NspErrorCode> getNspErrorCodeList() {
		return nspErrorCodeList;
	}

	public void setNspErrorCodeList(List<NspErrorCode> nspErrorCodeList) {
		this.nspErrorCodeList = nspErrorCodeList;
	}
	
	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}
	
	    public String getQuery_errorCode() {
        return this.query_errorCode;
    }
    
    public void setQuery_errorCode(String errorCode){
        this.query_errorCode = errorCode;
    }    

        public String getQuery_errorDesc() {
        return this.query_errorDesc;
    }
    
    public void setQuery_errorDesc(String errorDesc){
        this.query_errorDesc = errorDesc;
    }    

    
	
}
